package com.ac.scum.log.task.face.log.counsumer.stages;

import com.ac.common.core.id.SnowflakeIdGenerator;
import com.ac.scum.log.bean.Player;
import com.ac.scum.log.bean.ServerFTPLogChestOwnership;
import com.ac.scum.log.task.face.log.counsumer.LogConsumer;
import com.ac.scum.log.task.mapper.log.ServerFTPLogChestOwnershipMapper;
import com.ac.scum.log.task.utils.queue.FTPQueueItem;
import com.google.common.collect.Lists;
import io.micrometer.common.util.StringUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@Service
@AllArgsConstructor
public class ChestOwnershipLogConsumer implements LogConsumer {
    private final SnowflakeIdGenerator snowflakeIdGenerator;
    private final ServerFTPLogChestOwnershipMapper serverFTPLogChestOwnershipMapper;

    @Override
    public void consumerLog(FTPQueueItem ftpQueueItem) {
        try {
            Pattern pattern = Pattern.compile("(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{2}\\.\\d{2}\\.\\d{2}): Chest \\(entity id: (\\d+)\\) ownership changed: (?:\\[([^\\]]*)\\] (.*?))? ->(?: \\[([^\\]]*)\\] (.*))?");
            writeAllPlayerInfoInCache(ftpQueueItem);

            List<ServerFTPLogChestOwnership> serverFTPLogChestOwnerships = Lists.newArrayList();
            for (GameLogLine line : splitLine(ftpQueueItem)) {
                String lineContext = line.getLineContext();
                Matcher matcher = pattern.matcher(lineContext);
                ServerFTPLogChestOwnership serverFTPLogChestOwnership = new ServerFTPLogChestOwnership();
                serverFTPLogChestOwnership.setId(snowflakeIdGenerator.nextId());
                serverFTPLogChestOwnership.setServerId(ftpQueueItem.getServer().getId());
                serverFTPLogChestOwnership.setFileId(ftpQueueItem.getFileId());
                serverFTPLogChestOwnership.setLineStart(line.getStartByte());
                serverFTPLogChestOwnership.setLineEnd(line.getEndByte());
                serverFTPLogChestOwnership.setCreateTime(LocalDateTime.now());
                if (matcher.find()) {
                    String timestamp = matcher.group(1);
                    Long chestId = Long.parseLong(matcher.group(2));
                    long oldOwnerSteamId = matcher.group(3) != null ? Long.parseLong(matcher.group(3)) : 0L;
                    String oldOwnerName = matcher.group(4) != null ? matcher.group(4) : Strings.EMPTY;

                    if (oldOwnerSteamId != 0L) {
                        Player player = getOrInsertPlayerServer(oldOwnerSteamId, serverFTPLogChestOwnership.getServerId(), oldOwnerName);
                        serverFTPLogChestOwnership.setOwnerPlayerId(player.getId());
                        serverFTPLogChestOwnership.setOwnerPlayerSteamId(player.getSteamId());
                    }
                    if (StringUtils.isNotBlank(matcher.group(5))) {
                        Long newOwnerSteamId = Long.parseLong(matcher.group(5));
                        String newOwnerName = matcher.group(6);
                        Player player = getOrInsertPlayerServer(newOwnerSteamId, serverFTPLogChestOwnership.getServerId(), newOwnerName);
                        serverFTPLogChestOwnership.setNewPlayerId(player.getId());
                        serverFTPLogChestOwnership.setNewPlayerSteamId(player.getSteamId());
                    }
                    serverFTPLogChestOwnership.setServerTime(parseTimestamp(timestamp, ftpQueueItem));
                    serverFTPLogChestOwnership.setChestId(chestId);
                } else {
                    commitDiscardLog(ftpQueueItem, line, "chest_ownership");
                }
                serverFTPLogChestOwnerships.add(serverFTPLogChestOwnership);
            }
            serverFTPLogChestOwnerships.forEach(serverFTPLogChestOwnershipMapper::insert);
        } catch (Exception e) {
            log.error("箱子主人日志出现异常", e);
        }
    }
}
